import { UserPassword } from '@prisma/client';
import { TX } from '../db';

type CreateData = {
  userId: bigint;
  encryptedPassword: string;
};

async function create(tx: TX, { userId, encryptedPassword }: CreateData): Promise<UserPassword> {
  const now = new Date();
  return tx.userPassword.create({
    data: {
      userId,
      encryptedPassword,
      createTime: now,
      updateTime: now,
    },
  });
}

async function findByUserId(tx: TX, userId: bigint): Promise<UserPassword | null> {
  return tx.userPassword.findFirst({ where: { userId } });
}

export const userPasswordService = {
  create,
  findByUserId,
};
